﻿Imports System.Data.SqlClient

Public Class frmRIngresosDetallado
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal

    Private Sub frmRIngresosDetallado_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al Reporte de Liquidación")
        Usuario.Ubicacion("Módulo de Liquidación")
    End Sub

    Private Sub frmRIngresosDetallado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        datFecha1.Value = Now()


        lstA.Items.Clear()
        lstR.Items.Clear()
        cargaAutobuses()
        CargaRutas()
        RadioButton1.Checked = True

        Bitacora.RegistraActividad("Ingresó al Reporte de Detallado de Liquidaciones")
        Usuario.Ubicacion("Reporte de Detallado de Liquidaciones")
    End Sub
    Sub CargaRutas()
        lstRuta.Items.Clear()

        Dim cnconn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection

            strSql = "SELECT Nombre FROM RUTAS"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                Me.lstRuta.Items.Add(rdBuscar("Nombre").ToString.Trim)
            Loop
        Catch ex As SqlException
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub
    Sub cargaAutobuses()
        lstAutobus.Items.Clear()

        Dim cnconn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection

            strSql = "select NoEconomico from Autobuses where Estatus=1 ORDER BY NoEconomico"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader
            Dim primero As Boolean
            primero = True

            Do While rdBuscar.Read()
                Me.lstAutobus.Items.Add(rdBuscar("NoEconomico").ToString.Trim)
            Loop
        Catch ex As SqlException
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try

    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim aux As String = ""
        aux = lstRuta.Text.ToString.Trim
        If aux <> "" Then
            lstR.Items.Add(lstRuta.SelectedItem)
            lstRuta.Items.Remove(lstRuta.SelectedItem)
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim aux As String = ""
        aux = lstR.Text.ToString.Trim
        If aux <> "" Then
            lstRuta.Items.Add(lstR.SelectedItem)
            lstR.Items.Remove(lstR.SelectedItem)
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim aux As String = ""
        aux = lstAutobus.Text.ToString.Trim
        If aux <> "" Then
            lstA.Items.Add(lstAutobus.SelectedItem)
            lstAutobus.Items.Remove(lstAutobus.SelectedItem)
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim aux As String = ""
        aux = lstA.Text.ToString.Trim
        If aux <> "" Then
            lstAutobus.Items.Add(lstA.SelectedItem)
            lstA.Items.Remove(lstA.SelectedItem)
        End If
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        lstA.Enabled = False
        lstAutobus.Enabled = False
        lstR.Enabled = False
        lstRuta.Enabled = False
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        lstA.Enabled = False
        lstAutobus.Enabled = False
        lstR.Enabled = True
        lstRuta.Enabled = True
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        lstA.Enabled = True
        lstAutobus.Enabled = True
        lstR.Enabled = False
        lstRuta.Enabled = False
    End Sub
    Sub todos()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook
        Dim rng As Integer
        rng = 5

        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim strFecha1 As String = datFecha1.Value.ToString("MM/dd/yyyy")
        Dim strfecha2 As String = datFecha2.Value.ToString("MM/dd/yyyy")

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn

        strSql = " SELECT Fecha,NombreRuta,NoEconomico,Liquidacion" & _
                 " FROM Liquidacion " & _
                 " WHERE convert(varchar,fecha,101)>='" & strFecha1 & "'" & _
                 " AND convert(varchar,fecha,101)<='" & strfecha2 & "'" & _
                 " ORDER BY NombreRuta,NoEconomico"

        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader
        Dim strCamion As String = "?"

        Dim totalUB As Double = 0
        Dim TotalL As Double = 0
        Dim totalV As Double = 0

        Dim totalUBG As Double = 0
        Dim TotalLG As Double = 0
        Dim totalVG As Double = 0

        wb.SHEETS(1).columns(2).columnwidth = 14

        Dim DiaInicial As Date = datFecha1.Value.AddDays(-1)
        Dim strDia As String = ""
        Dim strAutobus As String = "?"
        Dim Distancia As Integer = 0
        Dim Dias As Integer = 0
        Dim FechaLiquidacion As Date
        Dim TotalDia(100) As Double
        Dim TotalDiaGeneral(100) As Double
        Dim Largo As Integer = 0
        Dim Col As Integer = 0
        Dim Primero As Boolean = True
        Dim Largo_ As Integer = 0
        Dim TotalAutobus As Double = 0
        Dim RenglonInicial As Integer
        Dim TotalColumna As Double = 0
        Dim TotalColumnaGeneral As Double = 0

        Do While rdBuscar.Read()
            If strCamion <> rdBuscar("NombreRuta").ToString.Trim Then
                If strCamion <> "?" Then
                    For i = 3 To Col - 1
                        Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
                        If Largo_ > 0 Then
                            TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
                        End If
                    Next
                    wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
                    wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
                    wb.SHEETS(1).cells(rng, Col).font.bold = True
                    TotalAutobus = 0
                    'Imprime total de columna total
                    For k = RenglonInicial To rng
                        TotalColumna = TotalColumna + wb.sheets(1).cells(k, Col).value
                    Next
                    TotalColumnaGeneral = TotalColumnaGeneral + TotalColumna
                    wb.sheets(1).cells(rng + 1, Col).value = TotalColumna
                    wb.SHEETS(1).cells(rng + 1, Col).font.italic = True
                    wb.SHEETS(1).cells(rng + 1, Col).font.bold = True
                    wb.SHEETS(1).cells(rng + 1, Col).NumberFormat = "#,##0.00"
                   
                    TotalColumna = 0
                    rng = rng + 1
                    wb.SHEETS(1).cells(rng, 2).value = "Total"
                    wb.SHEETS(1).cells(rng, 2).font.italic = True
                    wb.SHEETS(1).cells(rng, 2).font.bold = True
                    Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
                    For i = 1 To Dias + 1
                        wb.SHEETS(1).cells(rng, i + 2).value = TotalDia(i + 2)
                        wb.SHEETS(1).cells(rng, i + 2).font.italic = True
                        wb.SHEETS(1).cells(rng, i + 2).font.bold = True
                        wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
                        TotalDia(i + 2) = 0
                    Next
                    rng = rng + 2 ' Separa entre ruta
                End If
                Primero = True
                wb.SHEETS(1).cells(rng, 2).value = rdBuscar("NombreRuta").ToString.Trim
                wb.SHEETS(1).cells(rng, 2).font.size = 12
                wb.SHEETS(1).cells(rng, 2).font.bold = -1
                RenglonInicial = rng + 3
                'Arma encabezado
                rng = rng + 1
                Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
                wb.SHEETS(1).cells(rng + 1, 2).value = "Autobus"
                wb.SHEETS(1).cells(rng + 1, 2).FONT.name = "Segoe UI Light"
                wb.SHEETS(1).cells(rng + 1, 2).font.bold = True
                wb.SHEETS(1).CELLS(rng + 1, 2).HorizontalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, 2).VerticalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, 2).WrapText = True
                wb.SHEETS(1).CELLS(rng + 1, 2).Orientation = 0
                wb.SHEETS(1).CELLS(rng + 1, 2).AddIndent = False
                wb.SHEETS(1).CELLS(rng + 1, 2).IndentLevel = 0
                wb.SHEETS(1).CELLS(rng + 1, 2).ShrinkToFit = False
                wb.SHEETS(1).CELLS(rng + 1, 2).MergeCells = False
                wb.SHEETS(1).CELLS(rng + 1, 2).ReadingOrder = -5002
                For i = 1 To Dias + 1
                    wb.SHEETS(1).columns(i + 2).columnwidth = 12

                    wb.SHEETS(1).cells(rng, i + 2).font.bold = True
                    wb.SHEETS(1).CELLS(rng, i + 2).HorizontalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng, i + 2).VerticalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng, i + 2).WrapText = True
                    wb.SHEETS(1).CELLS(rng, i + 2).Orientation = 0
                    wb.SHEETS(1).CELLS(rng, i + 2).AddIndent = False
                    wb.SHEETS(1).CELLS(rng, i + 2).IndentLevel = 0
                    wb.SHEETS(1).CELLS(rng, i + 2).ShrinkToFit = False
                    wb.SHEETS(1).CELLS(rng, i + 2).MergeCells = False
                    wb.SHEETS(1).CELLS(rng, i + 2).ReadingOrder = -5002
                    DiaInicial = DiaInicial.AddDays(1)
                    wb.SHEETS(1).cells(rng, i + 2).value = DiaInicial.Date
                    Select Case DiaInicial.DayOfWeek
                        Case 1
                            strDia = "Lunes"
                        Case 2
                            strDia = "Martes"
                        Case 3
                            strDia = "Miercoles"
                        Case 4
                            strDia = "Jueves"
                        Case 5
                            strDia = "Viernes"
                        Case 6
                            strDia = "Sabado"
                        Case Else
                            strDia = "Domingo"
                    End Select
                    wb.SHEETS(1).cells(rng + 1, i + 2).value = strDia
                    wb.SHEETS(1).cells(rng + 1, i + 2).font.bold = True
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).HorizontalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).VerticalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).WrapText = True
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).Orientation = 0
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).AddIndent = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).IndentLevel = 0
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).ShrinkToFit = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).MergeCells = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).ReadingOrder = -5002
                Next
                wb.SHEETS(1).cells(rng + 1, Dias + 4).value = "Total"
                Col = Dias + 4
                wb.SHEETS(1).cells(rng + 1, Dias + 4).font.bold = True
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).HorizontalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).VerticalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).WrapText = True
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).Orientation = 0
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).AddIndent = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).IndentLevel = 0
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).ShrinkToFit = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).MergeCells = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).ReadingOrder = -5002
                DiaInicial = datFecha1.Value.AddDays(-1)
                rng = rng + 1
            End If
            strCamion = rdBuscar("NombreRuta").ToString.Trim
            If strAutobus <> rdBuscar("NoEconomico") Then
                If Primero = False Then
                    For i = 3 To Col - 1
                        Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
                        If Largo_ > 0 Then
                            TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
                        End If
                    Next
                    wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
                    wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
                    wb.SHEETS(1).cells(rng, Col).font.bold = True
                    TotalAutobus = 0
                End If
                rng = rng + 1
                wb.SHEETS(1).cells(rng, 2).value = rdBuscar("NoEconomico")

                Primero = False
            End If
            strAutobus = rdBuscar("NoEconomico")
            FechaLiquidacion = rdBuscar("Fecha")
            Distancia = DateDiff(DateInterval.Day, datFecha1.Value.Date, FechaLiquidacion.Date)
            wb.SHEETS(1).cells(rng, Distancia + 3).NumberFormat = "#,##0.00"
            Largo = Len(wb.SHEETS(1).cells(rng, Distancia + 3).value)
            If Largo > 0 Then
                wb.SHEETS(1).cells(rng, Distancia + 3).value = rdBuscar("Liquidacion") + wb.SHEETS(1).cells(rng, Distancia + 3).value
            Else
                wb.SHEETS(1).cells(rng, Distancia + 3).value = rdBuscar("Liquidacion")
            End If
            TotalDia(Distancia + 3) = TotalDia(Distancia + 3) + rdBuscar("Liquidacion")
            TotalDiaGeneral(Distancia + 3) = TotalDiaGeneral(Distancia + 3) + rdBuscar("Liquidacion")
        Loop

        For i = 3 To Col - 1
            Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
            If Largo_ > 0 Then
                TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
            End If
        Next
        wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
        wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
        wb.SHEETS(1).cells(rng, Col).font.bold = True

        'Imprime total de columna total
        For k = RenglonInicial To rng
            TotalColumna = TotalColumna + wb.sheets(1).cells(k, Col).value
        Next
        TotalColumnaGeneral = TotalColumnaGeneral + TotalColumna
        wb.sheets(1).cells(rng + 1, Col).value = TotalColumna
        wb.SHEETS(1).cells(rng + 1, Col).font.italic = True
        wb.SHEETS(1).cells(rng + 1, Col).font.bold = True
        wb.SHEETS(1).cells(rng + 1, Col).NumberFormat = "#,##0.00"

        rng = rng + 1
        wb.SHEETS(1).cells(rng, 2).value = "Total"
        wb.SHEETS(1).cells(rng, 2).font.italic = True
        wb.SHEETS(1).cells(rng, 2).font.bold = True
        Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
        For i = 1 To Dias + 1
            wb.SHEETS(1).cells(rng, i + 2).value = TotalDia(i + 2)
            wb.SHEETS(1).cells(rng, i + 2).font.italic = True
            wb.SHEETS(1).cells(rng, i + 2).font.bold = True
            wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
            TotalDia(i + 2) = 0
        Next
        rng = rng + 2 ' Separa entre ruta
        wb.SHEETS(1).cells(rng, 2).value = "Total General"
        wb.SHEETS(1).cells(rng, 2).font.italic = True
        wb.SHEETS(1).cells(rng, 2).font.bold = True
        Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
        For i = 1 To Dias + 1
            wb.SHEETS(1).cells(rng, i + 2).value = TotalDiaGeneral(i + 2)
            wb.SHEETS(1).cells(rng, i + 2).font.italic = True
            wb.SHEETS(1).cells(rng, i + 2).font.bold = True
            wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
            TotalDia(i + 2) = 0
        Next
        wb.SHEETS(1).cells(rng, Col).value = TotalColumnaGeneral
        wb.SHEETS(1).cells(rng, Col).font.italic = True
        wb.SHEETS(1).cells(rng, Col).font.bold = True
        wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Reporte detallado de liquidaciones del " & Me.datFecha1.Value.ToString("d") & " al " & Me.datFecha2.Value.ToString("d")
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        'Registra actividad


        If RadioButton1.Checked = True Then
            todos()
            Bitacora.RegistraActividad("Imprimió el reporte detallado de liquidaciones " & datFecha1.Text & " al dia " & datFecha2.Text)
        ElseIf RadioButton2.Checked = True Then
            If lstR.Items.Count = 0 Then
                MsgBox("Debes indicar al menos una ruta", MsgBoxStyle.Exclamation, "Aviso")
                Exit Sub
            End If
            Ruta()
            Bitacora.RegistraActividad("Imprimió el reporte detallado de liquidaciones " & datFecha1.Text & " al dia " & datFecha2.Text & ", Filtrado por Ruta")
        Else
            If lstA.Items.Count = 0 Then
                MsgBox("Debes indicar al menos un autobus", MsgBoxStyle.Exclamation, "Aviso")
                Exit Sub
            End If
            Autobus()
            Bitacora.RegistraActividad("Imprimió el reporte detallado de liquidaciones " & datFecha1.Text & " al dia " & datFecha2.Text & ", Filtrado por Autobus")
        End If

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Close()

    End Sub
    Sub Ruta()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook
        Dim rng As Integer
        rng = 5

        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim NuevaFecha1 As String = datFecha1.Value.ToString("yyyy-dd-MM")
        Dim NuevaFecha2 As String = datFecha2.Value.AddDays(1).ToString("yyyy-dd-MM")

        NuevaFecha1 = NuevaFecha1 & " 00:00:00"
        NuevaFecha2 = NuevaFecha2 & " 00:00:00"

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn

        Dim clasifi As Integer
        Dim t As Integer = lstR.Items.Count
        Dim strRutas As String = ""

        For i = 0 To t - 2
            clasifi = BuscarIntDatoCitra("RUTAS", "IdRuta", "Nombre='" & Me.lstR.Items(i).ToString.Trim & "'")
            strRutas = strRutas & clasifi & ","
        Next
        clasifi = BuscarIntDatoCitra("RUTAS", "IdRuta", "Nombre='" & Me.lstR.Items(t - 1).ToString.Trim & "'")
        strRutas = strRutas & clasifi

        strSql = " SELECT Fecha,NombreRuta,NoEconomico,Liquidacion" & _
                 " FROM Liquidacion " & _
                 " WHERE Fecha>='" & NuevaFecha1 & "'" & _
                 " AND Fecha<='" & NuevaFecha2 & "' AND IdRuta IN (" & strRutas & ") " & _
                 " ORDER BY NombreRuta,NoEconomico"

        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader
        Dim strCamion As String = "?"

        Dim totalUB As Double = 0
        Dim TotalL As Double = 0
        Dim totalV As Double = 0

        Dim totalUBG As Double = 0
        Dim TotalLG As Double = 0
        Dim totalVG As Double = 0

        wb.SHEETS(1).columns(2).columnwidth = 14

        Dim DiaInicial As Date = datFecha1.Value.AddDays(-1)
        Dim strDia As String = ""
        Dim strAutobus As String = "?"
        Dim Distancia As Integer = 0
        Dim Dias As Integer = 0
        Dim FechaLiquidacion As Date
        Dim TotalDia(100) As Double
        Dim TotalDiaGeneral(100) As Double
        Dim Largo As Integer = 0
        Dim Col As Integer = 0
        Dim Primero As Boolean = True
        Dim Largo_ As Integer = 0
        Dim TotalAutobus As Double = 0
        Dim RenglonInicial As Integer
        Dim TotalColumna As Double = 0
        Dim TotalColumnaGeneral As Double = 0

        Do While rdBuscar.Read()
            If strCamion <> rdBuscar("NombreRuta").ToString.Trim Then
                If strCamion <> "?" Then
                    For i = 3 To Col - 1
                        Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
                        If Largo_ > 0 Then
                            TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
                        End If
                    Next
                    wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
                    wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
                    wb.SHEETS(1).cells(rng, Col).font.bold = True
                    TotalAutobus = 0
                    'Imprime total de columna total
                    For k = RenglonInicial To rng
                        TotalColumna = TotalColumna + wb.sheets(1).cells(k, Col).value
                    Next
                    TotalColumnaGeneral = TotalColumnaGeneral + TotalColumna
                    wb.sheets(1).cells(rng + 1, Col).value = TotalColumna
                    wb.SHEETS(1).cells(rng + 1, Col).font.italic = True
                    wb.SHEETS(1).cells(rng + 1, Col).font.bold = True
                    wb.SHEETS(1).cells(rng + 1, Col).NumberFormat = "#,##0.00"

                    TotalColumna = 0
                    rng = rng + 1
                    wb.SHEETS(1).cells(rng, 2).value = "Total"
                    wb.SHEETS(1).cells(rng, 2).font.italic = True
                    wb.SHEETS(1).cells(rng, 2).font.bold = True
                    Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
                    For i = 1 To Dias + 1
                        wb.SHEETS(1).cells(rng, i + 2).value = TotalDia(i + 2)
                        wb.SHEETS(1).cells(rng, i + 2).font.italic = True
                        wb.SHEETS(1).cells(rng, i + 2).font.bold = True
                        wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
                        TotalDia(i + 2) = 0
                    Next
                    rng = rng + 2 ' Separa entre ruta
                End If
                Primero = True
                wb.SHEETS(1).cells(rng, 2).value = rdBuscar("NombreRuta").ToString.Trim
                wb.SHEETS(1).cells(rng, 2).font.size = 12
                wb.SHEETS(1).cells(rng, 2).font.bold = -1
                RenglonInicial = rng + 3
                'Arma encabezado
                rng = rng + 1
                Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
                wb.SHEETS(1).cells(rng + 1, 2).value = "Autobus"
                wb.SHEETS(1).cells(rng + 1, 2).FONT.name = "Segoe UI Light"
                wb.SHEETS(1).cells(rng + 1, 2).font.bold = True
                wb.SHEETS(1).CELLS(rng + 1, 2).HorizontalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, 2).VerticalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, 2).WrapText = True
                wb.SHEETS(1).CELLS(rng + 1, 2).Orientation = 0
                wb.SHEETS(1).CELLS(rng + 1, 2).AddIndent = False
                wb.SHEETS(1).CELLS(rng + 1, 2).IndentLevel = 0
                wb.SHEETS(1).CELLS(rng + 1, 2).ShrinkToFit = False
                wb.SHEETS(1).CELLS(rng + 1, 2).MergeCells = False
                wb.SHEETS(1).CELLS(rng + 1, 2).ReadingOrder = -5002
                For i = 1 To Dias + 1
                    wb.SHEETS(1).columns(i + 2).columnwidth = 12

                    wb.SHEETS(1).cells(rng, i + 2).font.bold = True
                    wb.SHEETS(1).CELLS(rng, i + 2).HorizontalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng, i + 2).VerticalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng, i + 2).WrapText = True
                    wb.SHEETS(1).CELLS(rng, i + 2).Orientation = 0
                    wb.SHEETS(1).CELLS(rng, i + 2).AddIndent = False
                    wb.SHEETS(1).CELLS(rng, i + 2).IndentLevel = 0
                    wb.SHEETS(1).CELLS(rng, i + 2).ShrinkToFit = False
                    wb.SHEETS(1).CELLS(rng, i + 2).MergeCells = False
                    wb.SHEETS(1).CELLS(rng, i + 2).ReadingOrder = -5002
                    DiaInicial = DiaInicial.AddDays(1)
                    wb.SHEETS(1).cells(rng, i + 2).value = DiaInicial.Date
                    Select Case DiaInicial.DayOfWeek
                        Case 1
                            strDia = "Lunes"
                        Case 2
                            strDia = "Martes"
                        Case 3
                            strDia = "Miercoles"
                        Case 4
                            strDia = "Jueves"
                        Case 5
                            strDia = "Viernes"
                        Case 6
                            strDia = "Sabado"
                        Case Else
                            strDia = "Domingo"
                    End Select
                    wb.SHEETS(1).cells(rng + 1, i + 2).value = strDia
                    wb.SHEETS(1).cells(rng + 1, i + 2).font.bold = True
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).HorizontalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).VerticalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).WrapText = True
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).Orientation = 0
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).AddIndent = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).IndentLevel = 0
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).ShrinkToFit = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).MergeCells = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).ReadingOrder = -5002
                Next
                wb.SHEETS(1).cells(rng + 1, Dias + 4).value = "Total"
                Col = Dias + 4
                wb.SHEETS(1).cells(rng + 1, Dias + 4).font.bold = True
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).HorizontalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).VerticalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).WrapText = True
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).Orientation = 0
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).AddIndent = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).IndentLevel = 0
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).ShrinkToFit = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).MergeCells = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).ReadingOrder = -5002
                DiaInicial = datFecha1.Value.AddDays(-1)
                rng = rng + 1
            End If
            strCamion = rdBuscar("NombreRuta").ToString.Trim
            If strAutobus <> rdBuscar("NoEconomico") Then
                If Primero = False Then
                    For i = 3 To Col - 1
                        Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
                        If Largo_ > 0 Then
                            TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
                        End If
                    Next
                    wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
                    wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
                    wb.SHEETS(1).cells(rng, Col).font.bold = True
                    TotalAutobus = 0
                End If
                rng = rng + 1
                wb.SHEETS(1).cells(rng, 2).value = rdBuscar("NoEconomico")

                Primero = False
            End If
            strAutobus = rdBuscar("NoEconomico")
            FechaLiquidacion = rdBuscar("Fecha")
            Distancia = DateDiff(DateInterval.Day, datFecha1.Value.Date, FechaLiquidacion.Date)
            wb.SHEETS(1).cells(rng, Distancia + 3).NumberFormat = "#,##0.00"
            Largo = Len(wb.SHEETS(1).cells(rng, Distancia + 3).value)
            If Largo > 0 Then
                wb.SHEETS(1).cells(rng, Distancia + 3).value = rdBuscar("Liquidacion") + wb.SHEETS(1).cells(rng, Distancia + 3).value
            Else
                wb.SHEETS(1).cells(rng, Distancia + 3).value = rdBuscar("Liquidacion")
            End If
            TotalDia(Distancia + 3) = TotalDia(Distancia + 3) + rdBuscar("Liquidacion")
            TotalDiaGeneral(Distancia + 3) = TotalDiaGeneral(Distancia + 3) + rdBuscar("Liquidacion")
        Loop

        For i = 3 To Col - 1
            Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
            If Largo_ > 0 Then
                TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
            End If
        Next
        wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
        wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
        wb.SHEETS(1).cells(rng, Col).font.bold = True

        'Imprime total de columna total
        For k = RenglonInicial To rng
            TotalColumna = TotalColumna + wb.sheets(1).cells(k, Col).value
        Next
        TotalColumnaGeneral = TotalColumnaGeneral + TotalColumna
        wb.sheets(1).cells(rng + 1, Col).value = TotalColumna
        wb.SHEETS(1).cells(rng + 1, Col).font.italic = True
        wb.SHEETS(1).cells(rng + 1, Col).font.bold = True
        wb.SHEETS(1).cells(rng + 1, Col).NumberFormat = "#,##0.00"

        rng = rng + 1
        wb.SHEETS(1).cells(rng, 2).value = "Total"
        wb.SHEETS(1).cells(rng, 2).font.italic = True
        wb.SHEETS(1).cells(rng, 2).font.bold = True
        Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
        For i = 1 To Dias + 1
            wb.SHEETS(1).cells(rng, i + 2).value = TotalDia(i + 2)
            wb.SHEETS(1).cells(rng, i + 2).font.italic = True
            wb.SHEETS(1).cells(rng, i + 2).font.bold = True
            wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
            TotalDia(i + 2) = 0
        Next
        rng = rng + 2 ' Separa entre ruta
        wb.SHEETS(1).cells(rng, 2).value = "Total General"
        wb.SHEETS(1).cells(rng, 2).font.italic = True
        wb.SHEETS(1).cells(rng, 2).font.bold = True
        Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
        For i = 1 To Dias + 1
            wb.SHEETS(1).cells(rng, i + 2).value = TotalDiaGeneral(i + 2)
            wb.SHEETS(1).cells(rng, i + 2).font.italic = True
            wb.SHEETS(1).cells(rng, i + 2).font.bold = True
            wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
            TotalDia(i + 2) = 0
        Next
        wb.SHEETS(1).cells(rng, Col).value = TotalColumnaGeneral
        wb.SHEETS(1).cells(rng, Col).font.italic = True
        wb.SHEETS(1).cells(rng, Col).font.bold = True
        wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Reporte detallado de liquidaciones del " & Me.datFecha1.Value.ToString("d") & " al " & Me.datFecha2.Value.ToString("d") & " (Fitrado por Ruta)"
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696
    End Sub
    Sub Autobus()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook
        Dim rng As Integer
        rng = 5

        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim strFecha1 As String = datFecha1.Value.ToString("MM/dd/yyyy")
        Dim strfecha2 As String = datFecha2.Value.ToString("MM/dd/yyyy")

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn

        Dim t As Integer = lstA.Items.Count
        Dim strBUSES As String = "'"
        For i = 0 To t - 2
            strBUSES = strBUSES & Me.lstA.Items(i).ToString.Trim & "','"
        Next
        strBUSES = strBUSES & Me.lstA.Items(t - 1).ToString.Trim & "'"

        strSql = " SELECT Fecha,NombreRuta,NoEconomico,Liquidacion" & _
                 " FROM Liquidacion " & _
                 " WHERE convert(varchar,fecha,101)>='" & strFecha1 & "'" & _
                 " AND convert(varchar,fecha,101)<='" & strfecha2 & "' AND NoEconomico IN (" & strBUSES & ") " & _
                 " ORDER BY NombreRuta,NoEconomico"

        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader
        Dim strCamion As String = "?"

        Dim totalUB As Double = 0
        Dim TotalL As Double = 0
        Dim totalV As Double = 0

        Dim totalUBG As Double = 0
        Dim TotalLG As Double = 0
        Dim totalVG As Double = 0

        wb.SHEETS(1).columns(2).columnwidth = 14

        Dim DiaInicial As Date = datFecha1.Value.AddDays(-1)
        Dim strDia As String = ""
        Dim strAutobus As String = "?"
        Dim Distancia As Integer = 0
        Dim Dias As Integer = 0
        Dim FechaLiquidacion As Date
        Dim TotalDia(100) As Double
        Dim TotalDiaGeneral(100) As Double
        Dim Largo As Integer = 0
        Dim Col As Integer = 0
        Dim Primero As Boolean = True
        Dim Largo_ As Integer = 0
        Dim TotalAutobus As Double = 0
        Dim RenglonInicial As Integer
        Dim TotalColumna As Double = 0
        Dim TotalColumnaGeneral As Double = 0

        Do While rdBuscar.Read()
            If strCamion <> rdBuscar("NombreRuta").ToString.Trim Then
                If strCamion <> "?" Then
                    For i = 3 To Col - 1
                        Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
                        If Largo_ > 0 Then
                            TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
                        End If
                    Next
                    wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
                    wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
                    wb.SHEETS(1).cells(rng, Col).font.bold = True
                    TotalAutobus = 0
                    'Imprime total de columna total
                    For k = RenglonInicial To rng
                        TotalColumna = TotalColumna + wb.sheets(1).cells(k, Col).value
                    Next
                    TotalColumnaGeneral = TotalColumnaGeneral + TotalColumna
                    wb.sheets(1).cells(rng + 1, Col).value = TotalColumna
                    wb.SHEETS(1).cells(rng + 1, Col).font.italic = True
                    wb.SHEETS(1).cells(rng + 1, Col).font.bold = True
                    wb.SHEETS(1).cells(rng + 1, Col).NumberFormat = "#,##0.00"

                    TotalColumna = 0
                    rng = rng + 1
                    wb.SHEETS(1).cells(rng, 2).value = "Total"
                    wb.SHEETS(1).cells(rng, 2).font.italic = True
                    wb.SHEETS(1).cells(rng, 2).font.bold = True
                    Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
                    For i = 1 To Dias + 1
                        wb.SHEETS(1).cells(rng, i + 2).value = TotalDia(i + 2)
                        wb.SHEETS(1).cells(rng, i + 2).font.italic = True
                        wb.SHEETS(1).cells(rng, i + 2).font.bold = True
                        wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
                        TotalDia(i + 2) = 0
                    Next
                    rng = rng + 2 ' Separa entre ruta
                End If
                Primero = True
                wb.SHEETS(1).cells(rng, 2).value = rdBuscar("NombreRuta").ToString.Trim
                wb.SHEETS(1).cells(rng, 2).font.size = 12
                wb.SHEETS(1).cells(rng, 2).font.bold = -1
                RenglonInicial = rng + 3
                'Arma encabezado
                rng = rng + 1
                Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
                wb.SHEETS(1).cells(rng + 1, 2).value = "Autobus"
                wb.SHEETS(1).cells(rng + 1, 2).FONT.name = "Segoe UI Light"
                wb.SHEETS(1).cells(rng + 1, 2).font.bold = True
                wb.SHEETS(1).CELLS(rng + 1, 2).HorizontalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, 2).VerticalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, 2).WrapText = True
                wb.SHEETS(1).CELLS(rng + 1, 2).Orientation = 0
                wb.SHEETS(1).CELLS(rng + 1, 2).AddIndent = False
                wb.SHEETS(1).CELLS(rng + 1, 2).IndentLevel = 0
                wb.SHEETS(1).CELLS(rng + 1, 2).ShrinkToFit = False
                wb.SHEETS(1).CELLS(rng + 1, 2).MergeCells = False
                wb.SHEETS(1).CELLS(rng + 1, 2).ReadingOrder = -5002
                For i = 1 To Dias + 1
                    wb.SHEETS(1).columns(i + 2).columnwidth = 12

                    wb.SHEETS(1).cells(rng, i + 2).font.bold = True
                    wb.SHEETS(1).CELLS(rng, i + 2).HorizontalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng, i + 2).VerticalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng, i + 2).WrapText = True
                    wb.SHEETS(1).CELLS(rng, i + 2).Orientation = 0
                    wb.SHEETS(1).CELLS(rng, i + 2).AddIndent = False
                    wb.SHEETS(1).CELLS(rng, i + 2).IndentLevel = 0
                    wb.SHEETS(1).CELLS(rng, i + 2).ShrinkToFit = False
                    wb.SHEETS(1).CELLS(rng, i + 2).MergeCells = False
                    wb.SHEETS(1).CELLS(rng, i + 2).ReadingOrder = -5002
                    DiaInicial = DiaInicial.AddDays(1)
                    wb.SHEETS(1).cells(rng, i + 2).value = DiaInicial.Date
                    Select Case DiaInicial.DayOfWeek
                        Case 1
                            strDia = "Lunes"
                        Case 2
                            strDia = "Martes"
                        Case 3
                            strDia = "Miercoles"
                        Case 4
                            strDia = "Jueves"
                        Case 5
                            strDia = "Viernes"
                        Case 6
                            strDia = "Sabado"
                        Case Else
                            strDia = "Domingo"
                    End Select
                    wb.SHEETS(1).cells(rng + 1, i + 2).value = strDia
                    wb.SHEETS(1).cells(rng + 1, i + 2).font.bold = True
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).HorizontalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).VerticalAlignment = -4108
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).WrapText = True
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).Orientation = 0
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).AddIndent = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).IndentLevel = 0
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).ShrinkToFit = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).MergeCells = False
                    wb.SHEETS(1).CELLS(rng + 1, i + 2).ReadingOrder = -5002
                Next
                wb.SHEETS(1).cells(rng + 1, Dias + 4).value = "Total"
                Col = Dias + 4
                wb.SHEETS(1).cells(rng + 1, Dias + 4).font.bold = True
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).HorizontalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).VerticalAlignment = -4108
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).WrapText = True
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).Orientation = 0
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).AddIndent = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).IndentLevel = 0
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).ShrinkToFit = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).MergeCells = False
                wb.SHEETS(1).CELLS(rng + 1, Dias + 4).ReadingOrder = -5002
                DiaInicial = datFecha1.Value.AddDays(-1)
                rng = rng + 1
            End If
            strCamion = rdBuscar("NombreRuta").ToString.Trim
            If strAutobus <> rdBuscar("NoEconomico") Then
                If Primero = False Then
                    For i = 3 To Col - 1
                        Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
                        If Largo_ > 0 Then
                            TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
                        End If
                    Next
                    wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
                    wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
                    wb.SHEETS(1).cells(rng, Col).font.bold = True
                    TotalAutobus = 0
                End If
                rng = rng + 1
                wb.SHEETS(1).cells(rng, 2).value = rdBuscar("NoEconomico")

                Primero = False
            End If
            strAutobus = rdBuscar("NoEconomico")
            FechaLiquidacion = rdBuscar("Fecha")
            Distancia = DateDiff(DateInterval.Day, datFecha1.Value.Date, FechaLiquidacion.Date)
            wb.SHEETS(1).cells(rng, Distancia + 3).NumberFormat = "#,##0.00"
            Largo = Len(wb.SHEETS(1).cells(rng, Distancia + 3).value)
            If Largo > 0 Then
                wb.SHEETS(1).cells(rng, Distancia + 3).value = rdBuscar("Liquidacion") + wb.SHEETS(1).cells(rng, Distancia + 3).value
            Else
                wb.SHEETS(1).cells(rng, Distancia + 3).value = rdBuscar("Liquidacion")
            End If
            TotalDia(Distancia + 3) = TotalDia(Distancia + 3) + rdBuscar("Liquidacion")
            TotalDiaGeneral(Distancia + 3) = TotalDiaGeneral(Distancia + 3) + rdBuscar("Liquidacion")
        Loop

        For i = 3 To Col - 1
            Largo_ = Len(wb.SHEETS(1).cells(rng, i).value)
            If Largo_ > 0 Then
                TotalAutobus = TotalAutobus + wb.SHEETS(1).cells(rng, i).value
            End If
        Next
        wb.SHEETS(1).cells(rng, Col).value = TotalAutobus
        wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"
        wb.SHEETS(1).cells(rng, Col).font.bold = True

        'Imprime total de columna total
        For k = RenglonInicial To rng
            TotalColumna = TotalColumna + wb.sheets(1).cells(k, Col).value
        Next
        TotalColumnaGeneral = TotalColumnaGeneral + TotalColumna
        wb.sheets(1).cells(rng + 1, Col).value = TotalColumna
        wb.SHEETS(1).cells(rng + 1, Col).font.italic = True
        wb.SHEETS(1).cells(rng + 1, Col).font.bold = True
        wb.SHEETS(1).cells(rng + 1, Col).NumberFormat = "#,##0.00"

        rng = rng + 1
        wb.SHEETS(1).cells(rng, 2).value = "Total"
        wb.SHEETS(1).cells(rng, 2).font.italic = True
        wb.SHEETS(1).cells(rng, 2).font.bold = True
        Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
        For i = 1 To Dias + 1
            wb.SHEETS(1).cells(rng, i + 2).value = TotalDia(i + 2)
            wb.SHEETS(1).cells(rng, i + 2).font.italic = True
            wb.SHEETS(1).cells(rng, i + 2).font.bold = True
            wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
            TotalDia(i + 2) = 0
        Next
        rng = rng + 2 ' Separa entre ruta
        wb.SHEETS(1).cells(rng, 2).value = "Total General"
        wb.SHEETS(1).cells(rng, 2).font.italic = True
        wb.SHEETS(1).cells(rng, 2).font.bold = True
        Dias = DateDiff(DateInterval.Day, datFecha1.Value, datFecha2.Value)
        For i = 1 To Dias + 1
            wb.SHEETS(1).cells(rng, i + 2).value = TotalDiaGeneral(i + 2)
            wb.SHEETS(1).cells(rng, i + 2).font.italic = True
            wb.SHEETS(1).cells(rng, i + 2).font.bold = True
            wb.SHEETS(1).cells(rng, i + 2).NumberFormat = "#,##0.00"
            TotalDia(i + 2) = 0
        Next
        wb.SHEETS(1).cells(rng, Col).value = TotalColumnaGeneral
        wb.SHEETS(1).cells(rng, Col).font.italic = True
        wb.SHEETS(1).cells(rng, Col).font.bold = True
        wb.SHEETS(1).cells(rng, Col).NumberFormat = "#,##0.00"

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Reporte detallado de liquidaciones del " & Me.datFecha1.Value.ToString("d") & " al " & Me.datFecha2.Value.ToString("d") & " (Fitrado por Autobus)"
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696
    End Sub
End Class